package com.sense.kukoo.biz_me.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.biz.FollowingsListItemBean
import com.sense.kukoo.common.repo.http.bean.UserSearchBean

class FollowingsListViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName


    private var type_isFans: Int = -1
    private var mPageIndex = 2
    private val mPageSize = 20

    private val mData: ArrayList<UserSearchBean> = ArrayList()
    private var mErrorRetry: ()->Unit = {}

    val action_back: MutableLiveData<Boolean> = MutableLiveData()
    val action_toChannelPage: MutableLiveData<UserSearchBean> = MutableLiveData()
    val action_network_error: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishRefres: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishLoadMore: MutableLiveData<Boolean> = MutableLiveData()
    val data_enableLoadMore:MutableLiveData<Boolean> = MutableLiveData()
    val data_followingsContent:MutableLiveData<ArrayList<FollowingsListItemBean>> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun setInitData(isFans: Int){
        type_isFans = isFans
    }

    fun onBack(){
        action_back.postValue(true)
    }

    private fun showData(){
        val list = arrayListOf<FollowingsListItemBean>()
        mData.forEach {
            list.add(FollowingsListItemBean(data = it))
        }
        data_followingsContent.postValue(list)
    }

    fun refreshList(isShowLoading: Boolean){
        if (isShowLoading) {
            toView(ACTION_SHOW_DIALOG)
        }
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mErrorRetry = { refreshList(isShowLoading) }
            action_network_error.postValue(true)
            if (isShowLoading){
                toView(ACTION_HIDE_DIALOG)
            }
        }
        request(onError) {
            val result = Repo.http.userSubscriptionPage(type_isFans, 1, mPageSize)
            if (result.isSuccess()) {
                result.data?.let {
                    mPageIndex = 2
                    mData.clear()
                    mData.addAll(it)
                    if(it.size < mPageSize){
                        data_enableLoadMore.postValue(false)
                    } else {
                        data_enableLoadMore.postValue(true)
                    }
                    action_finishRefres.postValue(true)
                    showData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mErrorRetry = { refreshList(isShowLoading) }
                action_network_error.postValue(true)
            }
            if (isShowLoading){
                toView(ACTION_HIDE_DIALOG)
            }
        }
    }

    fun loadMoreList(){
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mErrorRetry = { loadMoreList() }
            action_network_error.postValue(true)
        }
        request(onError) {
            val result = Repo.http.userSubscriptionPage(type_isFans, mPageIndex, mPageSize)
            if (result.isSuccess()) {
                result.data?.let {
                    mPageIndex++
                    mData.addAll(it)
                    if(it.size < mPageSize){
                        data_enableLoadMore.postValue(false)
                    } else {
                        data_enableLoadMore.postValue(true)
                    }
                    action_finishLoadMore.postValue(true)
                    showData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mErrorRetry = { loadMoreList() }
                action_network_error.postValue(true)
            }
        }
    }

    fun onBtnFollowClick(data: UserSearchBean){
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            sendHintMessage(com.sense.kukoo.common_i18n.R.string.search_no_net_toast)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            val subscription = Repo.http.subscription(data.id)
            if (subscription.isSuccess()) {
                data.isSub = if (data.isFollow()) 0 else 1
                showData()
            } else {
                Log.e(TAG, "result=" + subscription.msg)
                subscription.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun onBtnChannelClick(data: UserSearchBean){
        action_toChannelPage.postValue(data)
    }

    fun onErrorRetry(){
        mErrorRetry()
    }
}